package com.leoman.plz_p.views.homepage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.VideoListAdapter;
import com.leoman.plz_p.constants.Constants;
import com.leoman.plz_p.interfaces.VideoPlayView;
import com.leoman.plz_p.module.bean.VideoListBean;
import com.leoman.plz_p.presenter.VideoPlayPresenter;
import com.leoman.plz_p.utils.authority.AuthorityContext;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.com.corelib.base.BaseActivity;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

public class VideoPlayActivity extends BaseActivity<VideoPlayView, VideoPlayPresenter> implements VideoPlayView {


    @Bind(R.id.videoplayer)
    JZVideoPlayerStandard videoplayer;
    @Bind(R.id.tv_current)
    TextView tvCurrent;
    @Bind(R.id.tv_total)
    TextView tvTotal;
    @Bind(R.id.sk_progress)
    SeekBar skProgress;
    @Bind(R.id.iv_start)
    ImageView ivStart;
    @Bind(R.id.iv_video_list)
    ImageView ivVideoList;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @Bind(R.id.ls_video_list)
    ListView lsVideoList;

    private String courseId;
    private String courseChapterId;
    private String url;

    private VideoListAdapter adapter;
    private JZVideoPlayerStandard jzVideoPlayerStandard;
    private boolean isPlaying = false;

    public static Intent getLaunchIntent(Context context,String courseId, String courseChapterId,String url) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra(Constants.KEY_COURSE_ID, courseId);
        intent.putExtra(Constants.KEY_COURSE_CHAPTER_ID, courseChapterId);
        intent.putExtra(Constants.KEY_VIDEO_URL,url);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_video_paly;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        url=getIntent().getStringExtra(Constants.KEY_VIDEO_URL);
        courseId = getIntent().getStringExtra(Constants.KEY_COURSE_ID);
        courseChapterId = getIntent().getStringExtra(Constants.KEY_COURSE_CHAPTER_ID);
        jzVideoPlayerStandard = (JZVideoPlayerStandard) findViewById(R.id.videoplayer);
        jzVideoPlayerStandard.setTimeChangeListener(new JZVideoPlayer.onTimeChangeListener() {
            @Override
            public void onCurrentTimeChange(String time) {
                tvCurrent.setText(time);
            }

            @Override
            public void onTotalTimeChange(String time) {
                tvTotal.setText(time);
            }

            @Override
            public void onProgressChange(int progress) {
                skProgress.setProgress(progress);
                if (progress==100){
                    isPlaying=false;
                    ivStart.setBackgroundResource(R.mipmap.icon_video_start);
                    if (AuthorityContext.getContext().isLoggedIn())
                    getPresenter().videoWatchComplete(courseId,courseChapterId);

                }
            }

        });
        jzVideoPlayerStandard.setUp(url
                , JZVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, "");
        jzVideoPlayerStandard.batteryTimeLayout.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.progressBar.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.fullscreenButton.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.currentTimeTextView.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.totalTimeTextView.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.backButton.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.bottomProgressBar.setVisibility(View.INVISIBLE);
        jzVideoPlayerStandard.bottomContainer.setVisibility(View.INVISIBLE);

        adapter=new VideoListAdapter(getViewContext());
        lsVideoList.setAdapter(adapter);

        if (!AuthorityContext.getContext().isLoggedIn()){
            ivVideoList.setVisibility(View.GONE);
        }
    }

    @Override
    protected VideoPlayPresenter createPresenter() {
        return new VideoPlayPresenter();
    }


    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }


    @OnClick({R.id.iv_start, R.id.iv_video_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_start:
                if (skProgress.getProgress()==100&&!isPlaying){
                    jzVideoPlayerStandard.rePlay();
                    isPlaying=true;
                    ivStart.setBackgroundResource(R.mipmap.icon_video_pause);
                    return;
                }
                isPlaying = !isPlaying;
                jzVideoPlayerStandard.startButton.performClick();
                if (!isPlaying) {
                    ivStart.setBackgroundResource(R.mipmap.icon_video_start);
                } else {
                    ivStart.setBackgroundResource(R.mipmap.icon_video_pause);
                }
                break;
            case R.id.iv_video_list:
                getPresenter().getVideoList(courseChapterId);

                break;
        }
    }

    @Override
    public void obtainVideoList(final List<VideoListBean> list) {
        drawerLayout.openDrawer(Gravity.RIGHT);
        adapter.replaceAll(list);
        adapter.setOnItemClickListener(new VideoListAdapter.onItemClickListener() {
            @Override
            public void onVideoClickListener(int position) {
                startActivity(VideoPlayActivity.getLaunchIntent(getViewContext(),courseId,list.get(position).getId()+"",list.get(position).getUrl()));
                finish();
            }
        });
    }
}